
 /**
  * 显示添加/修改老师表单
  */
 loadTeacher()


 function loadTeacher(){
     /*清空搜索条件*/
     $("#teacherName").val("");
     $.ajax({
         url: "/api/teacher/list",

     }).done(function (data){
         let html = "";
         data.forEach((item, index)=>{
             html+=`
                <tr>
                    <td>${item.id}</td>
                    <td>${item.name}</td>
                    <td>${item.teachernum}</td>
                    <td>${item.age}</td>
                    <td>${item.sex}</td>
                    <td>${item.password}</td>
                    <td style="width: 160px"><button type="button" class="layui-btn layui-btn-sm layui-btn-normal" onclick="showTeacherDlg(${item.id})"><i class="layui-icon layui-icon-edit"></i> 编辑</button>
                    <button type="button" class="layui-btn layui-btn-sm layui-btn-danger" onclick="deleteById(${item.id})"><i class="layui-icon layui-icon-delete"></i> 删除</button></td>
                </tr>
            `
         })
         $("#teacherTb").html(html)
     })
 }
//删除功能
 function deleteById(id) {
     $.ajax({
         url:"/api/teacher/delete/"+id,
         method:"DELETE"
     }).done(result=>{
         console.log(result);
         alert("删除成功")
         loadTeacher();

     }).fail(error=>{
         alert("删除失败")
     })
 }

 // 搜索功能
 function searchTeacher() {
     // 获取输入框的值
     var teacherName = $("#teacherName").val();

     // 执行搜索操作，例如发送AJAX请求
     if (teacherName) {
         $.ajax({
             url: "/api/teacher/search",
             method: "GET",
             contentType: "application/json",
             data: { name: teacherName }, // 注意这里不需要 JSON.stringify
         }).done(function (data) {
             let html = "";
             data.forEach((item, index) => {
                 html += `
                <tr>
                    <td>${item.id}</td>
                    <td>${item.name}</td>
                    <td>${item.teachernum}</td>
                    <td>${item.age}</td>
                    <td>${item.sex}</td>
                    <td>${item.password}</td>
                    <td style="width: 160px"><button type="button" class="layui-btn layui-btn-sm layui-btn-normal" onclick="showTeacherDlg(${item.id})"><i class="layui-icon layui-icon-edit"></i> 编辑</button>
                    <button type="button" class="layui-btn layui-btn-sm layui-btn-danger" onclick="deleteById(${item.id})"><i class="layui-icon layui-icon-delete"></i> 删除</button></td>
                </tr>
            `;
             });
             $("#teacherTb").html(html);
         }).fail(function (error) {
             console.error("Error:", error);
         });
     } else {
         loadTeacher()
     }
 }
 // 为搜索按钮绑定点击事件
 /*$("#add").click(showTeacherDlg);*/
 $("#search").click(searchTeacher);
 $("#clear").click(loadTeacher)

 let layerIndex;

 /**
  * 新增或者修改老师信息
  * @param id
  */
 let row;
 function showTeacherDlg(id){
     let title="新增老师"
     if(id){
         //编辑
         title="编辑老师"
         //读取信息并赋值到表单
         row=id;
         $.ajax({
             url:"/api/teacher/"+id,
             method:"GET",
         }).done(result=>{
             console.log(result);
             $.each(result,function (key,value){
                 var field=$('#teacherForm [name="'+key+'"]')
                 if(field.is(':radio')){
                     field.filter('[value="'+value+'"]').prop("checked",true) //填充单选框
                 }else if(field.is(':checkbox')){
                     field.prop("checked",value==="yes");  //填充复选框
                 }else{
                     field.val(value) //填充其他字段
                 }
             })
         })


     }else{
         $("#teacherForm")[0].reset();
     }
     layerIndex=layer.open({
         type:1,
         area:['500px','auto'],
         title:title,
         content:$("#teacherForm")
     })
 }

 layui.use(function () {
     layui.form.on('submit(stu-dlg)', function (data) {
         event.preventDefault();
         commitStuDlg();
     })
 })

 function commitStuDlg(){
     let id=row
     let ip=$("#id").val();
     console.log(id)
     let url;
     let method;
     let formData = $("#teacherForm").serialize();
     if(id){
         url="/api/teacher/update";
         method="PUT";
         formData=formData+"&id="+id
     }else{
         url="/api/teacher/add";
         method="POST";
     }
     console.log(url)
     console.log(method)
     $.ajax({
         url:url,
         method:method,
         data:formData
     }).done(result=>{
         console.log(result);
         if(result.id){
             console.log("添加成功")
             loadTeacher();
             if(layerIndex){
                 layer.close(layerIndex);
             }
         }

     }).fail((jqXHR, textStatus, errorThrown)=>{
         console.log("添加失败"+textStatus+" "+errorThrown)
         alert("添加失败")
     });
 }




